package com.zuoshenge.sys.service;

import com.zuoshenge.sys.bean.JwtProperties;
import com.zuoshenge.sys.dto.OnlineUserDTO;
import com.zuoshenge.sys.entity.User;
import com.zuoshenge.sys.utils.JwtTokenUtil;
import com.zuoshenge.sys.utils.RedisUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Service
public class OnlineUserService {

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private JwtTokenUtil jwtTokenUtil;

    @Resource
    private JwtProperties jwtProperties;


    public void save(User user, String token){
        String loginKey = jwtTokenUtil.loginKey(token);
        redisUtil.set(loginKey, user.getUsername(), jwtProperties.getTokenValidityInSeconds(), TimeUnit.MILLISECONDS);
    }

    public String queryOnlineUsername(String username) {
        String loginKey = jwtTokenUtil.loginKey(username);
        return (String) redisUtil.get(loginKey);
    }

    public void kickOutForUsername(String username) {
        String loginKey = jwtTokenUtil.loginKey(username);
        redisUtil.del(loginKey);
    }

    public Long getPunishmentTtl(String username) {
        String punishmentKry = jwtTokenUtil.punishmentKey(username);
        return redisUtil.getExpireTtl(punishmentKry, TimeUnit.MILLISECONDS);
    }

    public void logout(String token) {
        String loginKey = jwtTokenUtil.loginKey(token);
        redisUtil.del(loginKey);
    }
}
